package at.fhooe.mc.duel_game.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import at.fhooe.mc.duel_game.*;

public class GameActivity extends Activity {
    GameLogic mGameLogic = GameLogic.getInstance();
    Handler handler;
    SensorManager mSensorManager;
    SensorService mSensorService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.layout_game_activity);
	
			
	handler = new Handler();

	// Init Sound
	SoundManager.getInstance();
	SoundManager.initSounds(this.getApplicationContext());
	SoundManager.loadSounds();

	this.getApplicationContext();
	// Register Sensor Listener
	mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
	Sensor sensor = mSensorManager
		.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	mSensorService = new SensorService();
	mSensorManager.registerListener(mSensorService, sensor,
		SensorManager.SENSOR_DELAY_FASTEST);
	mGameLogic.setGameActivity(this);
	mGameLogic.setHandler(handler);

	// start sync request
	if (GameLogic.getInstance().isServer())
	    GameLogic.getInstance().startSyncReq();
    }

    @Override
    protected void onResume() {
	// TODO Auto-generated method stub
	super.onResume();

	Intent i = getIntent();
	boolean isTraining = i.getBooleanExtra("TRAINING", false);
	GameLogic.getInstance().setTraining(isTraining);
	if (isTraining)
	    GameLogic.getInstance().startReload();

    }

    public void updateTextView(String text) {
	TextView textView = (TextView) findViewById(R.id.textViewGameStatus);
	textView.setText(text);
    }

    @Override
    protected void onDestroy() {
	// TODO Auto-generated method stub
	super.onDestroy();
	mSensorManager.unregisterListener(mSensorService);
    }

}
